import { _decorator, Component, instantiate, Node, Prefab, rect, Rect, Sprite, SpriteFrame, tween, UITransform, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('preBlockClick')
export class preBlockClick extends Component {
    private sprite_index: number = 0;
    private types_array: SpriteFrame[] = [];
    private temp_pos: Vec3 = null;
    original: preBlockClick = null;
    private time_for_add_layer3:number = 0;
    start() {

    }

    update(deltaTime: number) {

    }
    get_bounding_box(): Rect {
        return this.node.getComponent(UITransform).getBoundingBox();
    }
    get_custom_bounding_box(): Rect {
        let rec_1 = this.get_bounding_box();
        let num = 10;
        let rect_1 = this.get_bounding_box();
        let rect_2 = new Rect(rec_1.x + num, rec_1.y + num, rec_1.width - num * 2, rec_1.height - num * 2);
        return rect_2;
    }
    hide() {
        this.node.getChildByName("shadow").active = true;
    }
    show() {
        this.node.getChildByName("shadow").active = false;
    }
    init(sprite_index: number, types_array: SpriteFrame[]) {
        this.sprite_index = sprite_index;
        this.types_array = types_array;
    }
    refresh_Sprite(play_tween: boolean) {
        if (play_tween) {
            this.temp_pos = this.node.getPosition()
            tween(this.node)
                .to(0.2, { position: new Vec3(0, 0, 0) })
                .call(() => {
                    this.node.getChildByName('y_body').getComponent(Sprite).spriteFrame = this.types_array[this.sprite_index];
                })
                .to(0.2, { position: this.temp_pos })
                .start();
        }
        this.node.getChildByName('y_body').getComponent(Sprite).spriteFrame = this.types_array[this.sprite_index];
    }
    public get_sprite_index(): number {
        return this.sprite_index;
    }
    public set_sprite_index(index: number) {
        this.sprite_index = index;
    }
    can_touch(): boolean {
        return !this.node.getChildByName("shadow").active;
    }
    play_start_tween() {
        tween(this.node)
            .to(0.1, { scale: new Vec3(1.2, 1.2, 1) })
            .start();
    }
    play_end_tween() {
        tween(this.node)
            .to(0.1, { scale: new Vec3(1, 1, 1) })
            .start();
    }
    clone_block(parent: Node, pre_block: Prefab): preBlockClick {
        let new_block = instantiate(pre_block);
        let action = new_block.getComponent(preBlockClick);
        new_block.setParent(parent);
        let pos_world = this.node.getWorldPosition();
        let parent_local_pos = parent.getComponent(UITransform).convertToNodeSpaceAR(pos_world);
        new_block.setPosition(parent_local_pos);
        action.show();
        action.init(this.sprite_index, this.types_array);
        action.refresh_Sprite(false);
        action.original = this;
        this.node.active = false;
        return action;
    }
    set_temp_pos(v: Vec3) {
        this.temp_pos = v;
    }
    get_temp_pos(): Vec3 {
        return this.temp_pos;
    }
    set_add_layer3_time() {
        this.time_for_add_layer3 = new Date().getTime();
    }
    get_add_layer3_time():number{
        return this.time_for_add_layer3;
    }
}


